-- Author: Oskar

local WPP = surface.GetTextureID( "wpp/wpp" )
local Gradient = surface.GetTextureID( "vgui/gradient_down" )

/*==========================================================================
	Name:		GetActions
	Purpose: 	Get actions that are enabled on the server
===========================================================================*/
local function GetActions()
	Actions = {
		"Touch",
		"Grab",
		"Tool",
		"Duplicate",
		"Remove",
		"Use",
		"CPPI",
	}

	if ( GetConVarNumber("wpp_anti_damage") >= 1 ) then table.insert(Actions,"Damage") end
	if ( GetConVarNumber("wpp_anti_noclip") >= 1 ) then table.insert(Actions,"Noclip") end
	if ( GetConVarNumber("wpp_anti_colide") >= 1 ) then table.insert(Actions,"Collision") end

	return Actions
end

/*==========================================================================
	Name:		GetBuddyActions
	Purpose: 	Get actions that are enabled for buddys (limited/unlimited)
===========================================================================*/
local function GetBuddyActions(Limit)
	Actions = {
		"Touch",
		"Grab",
		"Tool",
		"Use",
	}
	
	if (Limit == false) then
		table.insert(Actions,"Duplicate")
		table.insert(Actions,"Remove")
		table.insert(Actions,"CPPI")
	end
	
	if ( GetConVarNumber("wpp_anti_damage") >= 1 ) then table.insert(Actions,"Damage") end
	if ( GetConVarNumber("wpp_anti_noclip") >= 1 ) then table.insert(Actions,"Noclip") end
	if ( GetConVarNumber("wpp_anti_colide") >= 1 ) then table.insert(Actions,"Collision") end
	
	return Actions
end

-- WPP_Base
local PANEL = {}

function PANEL:Init()
	
	self.Simple = true
	self.Players = {}
	self.Text = ""
	
	self:SetPos(100,100)
	
	self.CloseButton = vgui.Create( "DButton", self)
    function self.CloseButton:Paint()
        draw.SimpleText("r","Marlett",15/2,15/2,Color(255,0,0,255),1,1)
    end
    self.CloseButton:SetText("")
    self.CloseButton:SetPos(self:GetWide()-20,5)
    self.CloseButton:SetSize(15,15)
	self.CloseButton.DoClick = function()
		self:Remove()
	end
    
	self.SwitchButton = vgui.Create( "DButton", self )
	self.SwitchButton:SetText("Switch to advance menu")
	self.SwitchButton:SizeToContents()
	self.SwitchButton:SetPos(10,25/2-self.SwitchButton:GetTall()/2)
	
	function self.SwitchButton:Paint()
	end
	
	function self.SwitchButton:DoClick()
		if self:GetParent().Simple == true then
			self:GetParent().Simple = false
			self:SetText("Switch to simple menu")
			self:SizeToContents()
			self:SetPos(10,25/2-self:GetTall()/2)
		else
			self:GetParent().Simple = true
			self:SetText("Switch to advance menu")
			self:SizeToContents()
			self:SetPos(10,25/2-self:GetTall()/2)
		end
		self:GetParent():InvalidateLayout()
	end
	
	
	self:InvalidateLayout()
	
end


function PANEL:PerformLayout()
	
	local w,h = 400,20
	local x,y = 10,60
	
	
	if ( self.Simple == true ) then
		
		for k,Player in pairs( player.GetAll( ) ) do
			
			if ( Player ~= LocalPlayer( ) ) then
				
				if ( self.Players[k] ) then self.Players[k]:Remove( ) end
				
				self.Players[k] = vgui.Create( "WPP_Simple", self )
				self.Players[k]:SetPos( x, y )
				self.Players[k]:SetSize( w, h )
				self.Players[k]:SetPlayer( Player )
				y = y+h+10
				
			end
			
		end
		
		w = w+(x*2)
		h = y
		
	else
		
		w,h,x,y = 600,20,10,60
		
		for k,Player in pairs( player.GetAll( ) ) do
			
			if ( Player ~= LocalPlayer( ) ) then
				
				if ( self.Players[k] ) then self.Players[k]:Remove( ) end
				
				self.Players[k] = vgui.Create( "WPP_Advance", self )
				self.Players[k]:SetPos( x, y )
				self.Players[k]:SetSize( w, h )
				self.Players[k]:SetPlayer( Player )
				y = y+h+10
				
			end
			
		end
		
		w = w+(x*2)
		h = y
		
	end
	
	self:SetSize( w, h )
	self.CloseButton:SetPos( self:GetWide( ) - 20, 5 )
	self.SwitchButton:SizeToContents()
	self.SwitchButton:SetPos(10,25/2-self.SwitchButton:GetTall()/2)
	
end

function PANEL:Paint()
	local W,H = self:GetSize()
	
	surface.SetDrawColor(20,20,20)
	surface.DrawRect(0,0,W,H)
	
	surface.SetDrawColor(0,0,200,50)
	surface.SetTexture(Gradient)
	surface.DrawTexturedRect(0,0,W,H)
	
	surface.SetDrawColor(0,0,255)
	surface.DrawOutlinedRect(0,0,W,H)
	surface.DrawLine(0,25,W,25)
	surface.DrawLine(0,50,W,50)
	
	surface.SetFont("Trebuchet18")
	local Text = "Wolf Prop Protection 2"
	local x,y = surface.GetTextSize(Text)
	surface.SetTextColor(255,255,255,255)
	surface.SetTextPos(W/2-x/2,25/2-y/2)
	surface.DrawText(Text)
	
	surface.SetFont("Trebuchet18")
	local Text = "Name"
	local x,y = surface.GetTextSize(Text)
	surface.SetTextColor(255,255,255,255)
	surface.SetTextPos( 10, (25/2 - y/2)+25.5 )
	surface.DrawText(Text)
	
	if (self.Simple) then
		
		surface.SetFont("Trebuchet18")
		local Text = "Limited"
		local x,y = surface.GetTextSize(Text)
		surface.SetTextColor(255,255,255,255)
		local x2 = (W-20) - 15 - (1*75) + (70+(15/2)) - (x/2)
		surface.SetTextPos( x2, (25/2 - y/2)+25.5 )
		surface.DrawText(Text)
		
		surface.SetFont("Trebuchet18")
		local Text = "Unlimited"
		local x,y = surface.GetTextSize(Text)
		surface.SetTextColor(255,255,255,255)
		local x2 = (W-20) - 15 - (2*75) + (70+(15/2)) - (x/2)
		surface.SetTextPos( 10 + x2, (25/2 - y/2)+25.5 )
		surface.DrawText(Text)
		
	else
		surface.SetFont("Trebuchet18")
		
		for Key,Text in pairs( GetActions( ) ) do
			
			local x,y = surface.GetTextSize(Text)
			surface.SetTextColor(255,255,255,255)
			local x2 = (W-20) - 15 - (Key*55) + (50+(15/2)) - (x/2)
			surface.SetTextPos( 10 + x2, (25/2 - y/2)+25.5 )
			surface.DrawText(Text)
			
		end
		
	end
	
end
vgui.Register( "WPP_Base", PANEL, "DPanel" )

-- WWP_Simple
PANEL = {}

function PANEL:Init()
	self.Player = nil
	
	self.btn = vgui.Create( "WPP_Button", self)
	function self.btn:Click(State)
		RunConsoleCommand("wpp_buddy",tostring(true),tostring(State),self:GetParent().Player:UniqueID())
	end
    
	self.btnUnlimit = vgui.Create( "WPP_Button", self)
	function self.btnUnlimit:Click(State)
		RunConsoleCommand("wpp_buddy",tostring(false),tostring(State),self:GetParent().Player:UniqueID())
	end
    
	self.lblName = vgui.Create( "DLabel", self )
	self.lblName:SetFont("Trebuchet16")
	self.lblName:SetColor(Color(255,255,255,255))
	self.lblName:SetText("")
	self.lblName:SizeToContents()
	self.lblName:SetPos( 4, self:GetTall()/2 + self.lblName:GetTall()/2 )
	
end

function PANEL:SetPlayer(Player)
	self.Player = Player
	self.lblName:SetText( Player:Name() )
	self:InvalidateLayout()
end

function PANEL:PerformLayout()
	
	self.btn:SetPos( self:GetWide() - self.btn:GetWide() - 15, self:GetTall()/2 - self.btn:GetTall()/2 )
	self.btnUnlimit:SetPos( self:GetWide() - self.btn:GetWide() - 80, self:GetTall()/2 - self.btn:GetTall()/2 )
	
	self.lblName:SizeToContents()
	self.lblName:SetPos( 4, self:GetTall()/2 - self.lblName:GetTall()/2 )
	
	
	local function IsLimitedBuddy(Player)
		
		local n,m = 0,#GetBuddyActions(true)
		
		for _,Action in pairs( GetBuddyActions(true) ) do
			if LocalPlayer():WPP_HasTrust(Player,Action) then
				n = n+1
			end
		end
		
		return n == m
		
	end
	
	local function IsUnlimitedBuddy(Player)
		
		local n,m = 0,#GetBuddyActions(false)
		
		for _,Action in pairs( GetBuddyActions(false) ) do
			if LocalPlayer():WPP_HasTrust(Player,Action) then
				n = n+1
			end
		end
		
		return n == m
		
	end
	
	
	if (self.Player) then
		
		self.btn.State = false
		if (IsLimitedBuddy(self.Player)) then
			self.btn.State = true
		end
		if (IsUnlimitedBuddy(self.Player)) then
			self.btn.State = false
		end
		
		if (IsUnlimitedBuddy(self.Player)) then
			self.btnUnlimit.State = true
		else
			self.btnUnlimit.State = false
		end
		
		
	end
	
end

vgui.Register( "WPP_Simple", PANEL, "DPanel" )



-- WPP_Advance
PANEL = {}

function PANEL:Init()
	self.Player = nil
	
	self.lblName = vgui.Create( "DLabel", self )
	self.lblName:SetFont("Trebuchet16")
	self.lblName:SetColor(Color(255,255,255,255))
	self.lblName:SetText("")
	self.lblName:SizeToContents()
	self.lblName:SetPos( 4, self:GetTall()/2 + self.lblName:GetTall()/2 )
	
	for Key,Action in pairs( GetActions( ) ) do
		
		if !self["btn_"..Key] then
			self["btn_"..Key] = vgui.Create( "WPP_Button", self )
			
			self["btn_"..Key].Click = function(self)
				RunConsoleCommand("wpp_toggle",Action,self:GetParent().Player:UniqueID())
			end
			
			
		end
		
		if (self.Player) then 
			self["btn_"..Key].State = LocalPlayer():WPP_HasTrust( self.Player, Action )
		end
	end
	
	self:InvalidateLayout()
	
end

function PANEL:SetPlayer(Player)
	self.Player = Player
	self.lblName:SetText( Player:Name() )
	self:InvalidateLayout()
end

function PANEL:PerformLayout()
	
	for Key,Action in pairs( GetActions( ) ) do
		
		if !self["btn_"..Key] then
			self["btn_"..Key] = vgui.Create( "WPP_Button", self )
			
			self["btn_"..Key].Click = function(self,State)
				RunConsoleCommand("wpp_toggle",Action,self:GetParent().Player:UniqueID())
			end
			
		end
		
		if (self.Player) then 
			self["btn_"..Key].State = LocalPlayer():WPP_HasTrust( self.Player, Action )
		end
		
		self["btn_"..Key]:SetPos( self:GetWide() - self["btn_"..Key]:GetWide() - (Key*55) + 50, 20/2 -self["btn_"..Key]:GetTall()/2 )
	end
	
	self.lblName:SizeToContents()
	self.lblName:SetPos( 4, self:GetTall()/2 - self.lblName:GetTall()/2 )
	
end


vgui.Register( "WPP_Advance", PANEL, "DPanel" )


-- WPP_Button
PANEL = {}

function PANEL:Init()
	self:SetText("")
    self:SetSize(15,15)
	-- if (self.State == nil) then self.State = false end
end

function PANEL:DoClick()
	if self.State == true then
		self.State = false
	else
		self.State = true
	end
	if self.Click then self:Click(self.State) end
end

function PANEL:Paint()
	if self.State == nil then return end
	if self.State then 
		surface.SetFont("Marlett")
		local x,y = surface.GetTextSize("b")
		surface.SetTextPos( 15/2-x/2, 15/2-y/2 )
		surface.DrawText("b")
	else
		surface.SetFont("Marlett")
		local x,y = surface.GetTextSize("r")
		surface.SetTextPos( 15/2-x/2, 15/2-y/2 )
		surface.DrawText("r")
	end
end

vgui.Register( "WPP_Button", PANEL, "DButton" )




function OpenMenu()
	local Menu = vgui.Create("WPP_Base")
end

concommand.Add("wpp_menu",OpenMenu)











